package com.example.dome.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dome.domain.Dome;
import com.example.dome.domain.User;
import com.example.dome.mapper.DomeMapper;
import com.example.dome.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author lihanbo
 * @version 1.0
 * @date 2022/4/7 22:38
 */
@Api(tags = "分库分表、读写分离")
@RestController("/user")
@AllArgsConstructor
@Slf4j
public class UserController {

    private final UserMapper domeMapper;

    //shardingsphere 不会自己创建表， 需要自己编写 根据时间范围创建表的方法
    //分表查询过程中表要是不存在会报错，必须提前生成对应的表

    @ApiOperation("分表后查询")
    @GetMapping("/list")
    public Object list() {
        //分表后查询必须要使用 between进行查询
        return domeMapper.selectPage(new Page<User>(1, 15), new QueryWrapper<User>());
//        return domeMapper.selectPage(new Page<Dome>(1, 15), new QueryWrapper<Dome>().lambda()
//                .between(Dome::getCreateDate, "2021-01-01 00:00:00", "2022-03-01 23:59:59"));
    }

    @ApiOperation("分表后添加")
    @GetMapping("/add")
    public Object add() {
        //分表是跟进创建时间， 创建时间必须要有值， 也可以使用mybatis的自动填充功能
        User entity = new User();
        entity.setUserName(UUID.fastUUID().toString());
        entity.setId(getId(entity.getUserName(),System.currentTimeMillis()));
        entity.setCreateDate(new Date());
        domeMapper.insert(entity);
        return entity;
    }

    @ApiOperation("get")
    @GetMapping("/get/{id}")
    public Object get(@PathVariable Long id) {
        return domeMapper.selectById(id);
    }

    @ApiOperation("getByName")
    @GetMapping("/getByName/{name}")
    public Object get(@PathVariable String name) {
        return domeMapper.selectList(new LambdaQueryWrapper<User>().eq(User::getUserName,name));
    }

    public static long getId(String code,Long id){
        StringBuilder customerId = new StringBuilder(id.toString());
        int hashCode = Math.abs(code.hashCode());
        if(hashCode<1000){
            customerId.append(String.format("%04d",hashCode));
            return Long.valueOf(customerId.toString());
        }
        String s = String.valueOf(hashCode);
        String s1 = s.substring(s.length() - 4);
        customerId.append(s1);
        return Long.valueOf(customerId.toString());
    }

    @ApiOperation("分表后更新")
    @GetMapping("/update/{id}")
    public Object update(@PathVariable Long id) {
        //分表后更新， 必须要知道对应数据的创建年月，也就是between才能出发分表的操作
        User entity = new User();
        entity.setId(id);
        entity.setCreateDate(new Date());
        domeMapper.updateById(entity);
        return entity;
    }

    @ApiOperation("分表后删除")
    @GetMapping("/delete/{id}")
    public Object delete(@PathVariable Long id) {
        return domeMapper.deleteById(id);
    }

}
